package cn.zhentao.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtil {
    private static Properties prop;
    private static String driverClassName;
    private static String url;
    private static String username;
    private static String password;
    static{
        //加载驱动
        try {
            prop = new Properties();
            //读取配置文件
            prop.load(DBUtil.class.getClassLoader().getResourceAsStream("db.properties"));
            driverClassName = prop.getProperty("driverClassName");
            url = prop.getProperty("url");
            username=prop.getProperty("username");
            password = prop.getProperty("password");

            Class.forName(driverClassName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    //创建连接方法
    public static Connection getConn() throws SQLException {
        return DriverManager.getConnection(url,username,password);
    }

    public static void main(String[] args) throws SQLException {
        System.out.println(getConn());
    }
}
